{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aa0e9e6b",
   "metadata": {},
   "source": [
    "# langchain提示词模板"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a3463c5",
   "metadata": {},
   "source": [
    "#### 一、字符串提示词模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24963890",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "df00efaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = PromptTemplate.from_template(\n",
    "    \"请你以下面的主题，给我讲个笑话，不超过50个字。\\n\\n{topic}\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1a2a82ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StringPromptValue(text='请你以下面的主题，给我讲个笑话，不超过50个字。\\n\\n秋天')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_template.invoke({\"topic\": \"秋天\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0055caf",
   "metadata": {},
   "source": [
    "#### 二、聊天提示词模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "87150f06",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f03e9f32",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate([\n",
    "    (\"system\", \"你是一个乐于助人的AI助手\"),\n",
    "    (\"user\", \"给我讲一个关于{topic}的笑话。\")\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e0750b18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='你是一个乐于助人的AI助手', additional_kwargs={}, response_metadata={}), HumanMessage(content='给我讲一个关于cats的笑话。', additional_kwargs={}, response_metadata={})])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt.invoke({\"topic\": \"cats\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fb683db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "learn_langchain",
   "language": "python",
   "name": "learn_langchain"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
